package com.jyw.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jyw.R;
import com.jyw.adapter.TimePickerHAdapter;
import com.jyw.adapter.TimePickerVAdapter;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.Address;
import com.jyw.pojo.EventBusTypes;
import com.jyw.pojo.UserInfo;
import com.jyw.thirdparty.DividerGridItemDecoration;

import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/10
 */

public class TimePicker extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler0, recycler1;
    private TextView btnGo;

    private TimePickerVAdapter vAdapter;
    private TimePickerHAdapter hAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_time_picker);
        recycler0 = (RecyclerView) findViewById(R.id.recycler0);
        recycler1 = (RecyclerView) findViewById(R.id.recycler1);

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        GridLayoutManager mgr1 = new GridLayoutManager(TimePicker.this, 3);
        recycler1.setLayoutManager(mgr1);
        recycler1.addItemDecoration(new DividerGridItemDecoration(TimePicker.this, 3));
        recycler1.setAdapter(vAdapter = new TimePickerVAdapter(TimePicker.this));

        LinearLayoutManager mgr0 = new LinearLayoutManager(TimePicker.this);
        mgr0.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycler0.setLayoutManager(mgr0);
        hAdapter = new TimePickerHAdapter(TimePicker.this);
        hAdapter.setOnDateSelectedListner(new TimePickerHAdapter.OnDateSelectedListner() {
            @Override
            public void onSelected(boolean isToday) {
                if (isToday) {
                    vAdapter.swapToToDay(true);
                } else {
                    vAdapter.swapToToDay(false);
                }
            }
        });
        recycler0.setAdapter(hAdapter);

        btnGo = (TextView) findViewById(R.id.btn_go);
        findViewById(R.id.iv_back).setOnClickListener(this);
        btnGo.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go:
                if (vAdapter.getFinalTime().equals("尚未选择")) {
                    Toast.makeText(TimePicker.this, "尚未选择时间", Toast.LENGTH_SHORT).show();
                } else {
                    Calendar calendar = Calendar.getInstance(Locale.CHINA);
                    String result = calendar.get(Calendar.YEAR) + "年" + hAdapter.getFinalDate() + "-" + vAdapter.getFinalTime();
                    EventBus.getDefault().post(new EventBusTypes.DateTimeSelected(result));
                    finish();
                }
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
